package cn.zgr.util;

import org.apache.commons.dbcp.BasicDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBUtils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    private static BasicDataSource dataSource;

    static {
        Properties prop = new Properties();
        InputStream ips = DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            prop.load(ips);
            driver = prop.getProperty("driver");
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");

            dataSource = new BasicDataSource();
            dataSource.setDriverClassName(driver);
            dataSource.setUrl(url);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            dataSource.setInitialSize(3);
            dataSource.setMaxActive(3);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                ips.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static Connection getConn() throws Exception {
        System.out.println("dataSource: " + dataSource.getUsername() + dataSource.getUrl());

        Connection conn = dataSource.getConnection();
        return conn;
    }

    public static void colse(Connection conn, Statement stat, ResultSet rs){
        try {
            if (rs != null){
                rs.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        try {
            if (stat != null){
                stat.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        try {
            if (conn != null){
                conn.setAutoCommit(true);
                conn.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public static void close(Connection conn, PreparedStatement stat, ResultSet rs) {
    }
}
